package com.singzi.leetcode.N500;

public class T0467 {
    public static void main(String[] args) {

        String s = "zabcabc";
        Solution0467 solution0467 = new Solution0467();
        System.out.println(solution0467.findSubstringInWraproundString(s));

    }
}
class Solution0467 {
    public int findSubstringInWraproundString(String p) {
        int []dp = new int[p.length()];
        int []dps = new int[p.length()];
        dp[0] = 1;
        dps[0] = 1;
        boolean[][]visit = new boolean[26][p.length()];
        visit[p.charAt(0) - 'a'][0] = true;

        for(int i = 1; i < p.length();i++){
            int num = p.charAt(i) - p.charAt(i-1), index = p.charAt(i) -'a';
            if(num == -25 || num == 1){
                if(!visit[index][dps[i-1]]){
                    dp[i] = dps[i-1] + 1;
                    visit[index][dps[i-1]] = true;
                    for(int j = 0;j < dps[i-1];j++){
                        visit[index][j] = true;
                    }
                }

                dps[i] = dps[i-1] + 1;
            }else{
                dps[i] = 1;
                if(!visit[index][0]) {
                    dp[i] = 1;
                    visit[index][0] = true;
                }
            }
        }
        int cont = 0;
        for(int num:dp){
            cont += num;
        }
        return cont;
    }
}
